home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / mgr / sparcmgr / doc.zoo / doc / usrman / croff / croff.l < prev    next >
Encoding:
Lex Description  |  1989-01-24  |  5.9 KB  |  234 lines

  1. %{
  2. /*
  3.                          Copyright (c) 1988 Bellcore
  4.                              All Rights Reserved
  5.         Permission is granted to copy or use this program, EXCEPT that it
  6.         may not be sold for profit, the copyright notice must be reproduced
  7.         on copies, and credit should be given to Bellcore where it is due.
  8.         BELLCORE MAKES NO WARRANTY AND ACCEPTS NO LIABILITY FOR THIS PROGRAM.
  9.  
  10.     $Header: croff.l,v 1.2 88/07/07 10:17:25 sau Exp $
  11.     $Source: /tmp/mgrsrc/doc/usrman/croff/RCS/croff.l,v $
  12. */
  13. static char    RCSid_[] = "$Source: /tmp/mgrsrc/doc/usrman/croff/RCS/croff.l,v $$Revision: 1.2 $";
  14.  
  15. /* is this lex abuse? */
  16.  
  17.  
  18. #include "hash.h"
  19.  
  20. #define dprintf    if(debug)fprintf
  21.  
  22. extern struct table_entry keywords_data[];
  23. extern struct table_entry *keywords[];
  24.  
  25. char *ks,*ke;    /* keyword delimeters */
  26. char *cs,*ce;    /* comment delimeters */
  27. char *is,*ie;    /* identifier delimeters */
  28. char *fs,*fe;    /* function delimeters */
  29. char *ps,*pe;    /* pre-proc delimeters */
  30.  
  31. int force = 0;        /* force xlation without .SS */
  32. int no_troff = 0;    /* dont xlate \ etc */
  33. int no_lig = 0;        /* on't emit troff ligatures (brain damaged imagen) */
  34. int debug = 0;        /* do debugging */
  35.  
  36. %}
  37.  
  38. %START start comment quote
  39. W    [     ]*
  40. K    [a-zA-Z_][a-zA-Z0-9_]*
  41.  
  42. %%
  43.  
  44.  
  45. ^".SS".*$ {            /* start source processing */
  46.         static char t1[25],t2[25],t3[25],t4[25];
  47.     int n;
  48.     BEGIN start;
  49.         if (!force) {
  50.        ECHO;
  51.        n = sscanf(yytext,".SS %s %s %s %s",t1,t2,t3,t4);
  52.            switch(n) {
  53.               case 4: case 5: case 6: case 7: case 8: case 9:
  54.                  ce = t4;
  55.               case 3:
  56.                  ke = t3;
  57.               case 2:
  58.                  cs = t2;
  59.               case 1:
  60.                  ks = t1;
  61.               }
  62.        }
  63.     }
  64. <start,comment,quote>^".SE" {    /* end source processing */
  65.     if (!force)
  66.            BEGIN 0;
  67.     ECHO;
  68.     }
  69. <start>"/*" {            /* start a comment */
  70.     BEGIN comment;
  71.     printf("%s",cs);
  72.     ECHO;
  73.     dprintf(stderr,"[CS]");
  74.     }
  75. <comment>"*/" {            /* end a comment */
  76.     ECHO;
  77.     printf("%s",ce);
  78.     BEGIN start;
  79.     dprintf(stderr,"[CE]");
  80.     }
  81. <start,comment,quote>"\-|" {    /* handle troff escapes */
  82.     if (no_troff)
  83.            ECHO;
  84.     else switch(*yytext) {
  85.            case '-':  printf("\\-");   break;
  86.        case '\\': printf("\\e");   break;
  87.        case '|':  printf("\\(or"); break;
  88.            }
  89.     dprintf(stderr,"%s",yytext);
  90.     }
  91. <start>\" {            /* start quote */
  92.     BEGIN quote;
  93.     ECHO;
  94.     dprintf(stderr,"[\"");
  95.     }
  96. <quote>\\\" {            /* \" in a string */
  97.     printf("\\e\"");
  98.     }
  99. <quote>\" {            /* end quoted string */
  100.     BEGIN start;
  101.     ECHO;
  102.     dprintf(stderr,"\"]");
  103.     }
  104. <start,comment,quote>[fF][il]    |
  105. <start,comment,quote>ff {    /* defeat troff ligatures */
  106.     if (no_lig)
  107.        printf("%c\\&%c",yytext[0],yytext[1]);
  108.     else
  109.            ECHO;
  110.     dprintf(stderr,"[%s]\n",yytext);
  111.     }
  112. <start>^#{W}define    |
  113. <start>^#{W}undef    |
  114. <start>^#{W}line    |
  115. <start>^#{W}include    |
  116. <start>^#{W}ifdef    |
  117. <start>^#{W}ifndef    |
  118. <start>^#{W}if        |
  119. <start>^#{W}endif    |
  120. <start>^#{W}else {        /* pre-processor tokens */
  121.        printf("%s%s%s",ps,yytext,pe);
  122.        dprintf(stderr,"%s%s%s","[PS]",yytext,"[PE]");
  123.     }
  124. <start>{K}/{W}\( {        /* look for key words  - might be functions */
  125.     if (is_entry(keywords,HSIZE,yytext)) 
  126.        printf("%s%s%s",ks,yytext,ke);
  127.      else
  128.        printf("%s%s%s",fs,yytext,fe);
  129.     }
  130. <start>{K} {            /* look for key words  - might be identifiers */
  131.     if (is_entry(keywords,HSIZE,yytext)) 
  132.        printf("%s%s%s",ks,yytext,ke);
  133.      else
  134.        printf("%s%s%s",is,yytext,ie);
  135.     }
  136. %%
  137.  
  138. #define GET_OPT(i)    \
  139.     strlen(argv[i])>2 ? argv[i]+2 : argv[++i]
  140.  
  141. main(argc,argv)
  142. int argc;
  143. char **argv;
  144.    {
  145.    register int i;
  146.    register char c = '\0';
  147.  
  148.    /* set default values */
  149.  
  150.    ks = "\\fB";        /* keywords in bold */
  151.    ke = "\\fP";
  152.    cs = "\\fI";        /* comments in bold */
  153.    ce = "\\fP";
  154.    is = "";
  155.    ie = "";
  156.    fs = "";
  157.    fe = "";
  158.    ps = "\\fB";        /* cpp decl's in bold */
  159.    pe = "";
  160.  
  161.    debug = getenv("DEBUG");
  162.  
  163.    /* get arguments */
  164.  
  165.    for(i=1;i<argc;i++) {
  166.       if (*argv[i] == '-')
  167.          switch (c = argv[i][1]) {
  168.             case 'a':    /* add keyword to list */
  169.                add_entry(keywords,HSIZE,GET_OPT(i));
  170.                break;
  171.             case 'd':    /* delete keyword from list */
  172.                dlt_entry(keywords,HSIZE,GET_OPT(i));
  173.                break;
  174.             case 'F':    /* force processing without .SS */
  175.                unput('\n');unput('S'); unput('S'); unput('.');
  176.                force++;
  177.                break;
  178.             case 'l':    /* don't pass any ligatures to troff */
  179.                no_lig++;    
  180.                break;
  181.             case 't':    /* don't futz with \, | or - \n */
  182.                no_troff++;    
  183.                break;
  184.             case 'p':    /* set pre-processor escape prefix */
  185.                ps = GET_OPT(i);
  186.                break;
  187.             case 'i':    /* set identifier escape prefix */
  188.                is = GET_OPT(i);
  189.                break;
  190.             case 'f':    /* set function escape prefix */
  191.                fs = GET_OPT(i);
  192.                break;
  193.             case 'k':    /* set keyword escape prefix */
  194.                ps = ks = GET_OPT(i);
  195.                break;
  196.             case 'c':    /* set comment escape prefix */
  197.                cs = GET_OPT(i);
  198.                break;
  199.             case 'e':    /* set all ending escape prefixen */
  200.                ie = pe = fe = ke = ce = GET_OPT(i);
  201.                break;
  202.             default:
  203.                fprintf(stderr,"%s: flag %s ignored\n",*argv,argv[i]);
  204.                break;
  205.             }
  206.          else switch(c) {    /* optional turn-off codes */
  207.             case 'p':
  208.                pe = argv[i];
  209.                break;
  210.             case 'i':
  211.                ie = argv[i];
  212.                break;
  213.             case 'f':
  214.                fe = argv[i];
  215.                break;
  216.             case 'k':
  217.                pe = ke = argv[i];
  218.                break;
  219.             case 'c':
  220.                ce = argv[i];
  221.                break;
  222.             default:
  223.                fprintf(stderr,"%s: arg [%s] ignored\n",*argv,argv[i]);
  224.                break;
  225.             }
  226.       }
  227.    yylex();
  228.    }
  229.  
  230. yywrap()
  231.    {
  232.    return(1);
  233.    }
  234.